package demo_01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertDemo_01 {
    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
//        数据库名在localhost上存在才可以连接
        String url = "jdbc:mysql://localhost:3306/jdbc" +
                "?useUnicode=true&characterEncoding=utf-8";
        String user = "root";
        String password = "admin";
//        声明数据库连接需要的参数
        try {
            Class.forName(driver);
//            加载驱动类
        } catch (ClassNotFoundException e) {
            System.out.println("驱动类不存在!");
            e.printStackTrace();
        }
        try {
            Connection conn = DriverManager.
                    getConnection(url,user,password);
            String sql = "INSERT INTO tbl_book_info(b_no,b_name,b_price)\n" +
                    "VALUES('123','java基础',56)";
            Statement stmt = conn.createStatement();
            int r = stmt.executeUpdate(sql);//执行修改
            if (r>0){
                System.out.println("执行成功");
            }else {
                System.out.println("执行失败");
            }
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {

        }

    }
}
